const mongoose = require('mongoose');
const Category = require('./category');
const Tag = require('./tagSchema');
// 定义 postSchema
const postSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        trim: true,
        maxlength: 100
    },
    content: {
        type: String,
        required: true
    },
    excerpt: {
        type: String,
        maxlength: 200
    },
    coverImage: {
        type: String
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    categories: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Category'
    }],
    tags: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Tag'
    }],
    status: {
        type: String,
        enum: ['draft', 'published', 'archived'],
        default: 'draft'
    },
    viewCount: {
        type: Number,
        default: 0
    },
    likeCount: {
        type: Number,
        default: 0
    },
    commentCount: {
        type: Number,
        default: 0
    },
    isFeatured: {
        type: Boolean,
        default: false
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    },
    publishedAt: {
        type: Date
    }
});

// 自动生成摘要
postSchema.pre('save', function (next) {
    if (this.content && !this.excerpt) {
        this.excerpt = this.content.substring(0, 200) + '...';
    }
    next();
});

// 创建并导出 Post 模型
const Post = mongoose.model('Post', postSchema);
module.exports = Post;    